import http from '@ohos.net.http';

export async function get100kFile() {
  let num: number = 0;
  for (var index = 0; index < 300000; index++) {
    num = index - 1;
  }
  let httpRequest = http.createHttp();
  httpRequest.request(
    "https://www.baidu.com/",
    {
      method: http.RequestMethod.GET,
      connectTimeout: 5000,
      readTimeout: 5000,
    }, (err, data) => {
    if (!err) {

      // let s: String = String.fromCharCode.apply(null, new Uint8Array(data.result));
      let s: String = "";
      (new Uint8Array(Number(data.result))).forEach( (byte: number) => {
        s += String.fromCharCode(byte);
      });
      s.length;
      return 0; // 8+
    } else {
      httpRequest.destroy();
      return 0;
    }
    return 0;
  }
  );
  return 0;
}

export async function post100kFile() {

  let httpRequest = http.createHttp();
  httpRequest.request(
    "https://www.baidu.com/",
    {
      method: http.RequestMethod.POST,
      header: {
        'Content-Type': 'application/json'
      },
      extraData: {
        "data": "data to send"
      },
      connectTimeout: 60000,
      readTimeout: 60000,
    }, (err, data) => {
    if (!err) {
      return '0';
    } else {
      httpRequest.destroy();
      return '0'
    }
  }
  );
  return '0'
}